package com.web.service.impl;

import com.web.mapper.UserMapper;
import com.web.pojo.User;
import com.web.pojo.UserExample;
import com.web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author web
 * @version 1.0
 * @date 2024/04/13 23:58
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(user.getUsername()).andPasswordEqualTo(user.getPassword());
        List<User> list = userMapper.selectByExample(userExample);
        return list.size() != 0 ? list.get(0) : null;
    }

    @Override
    public Integer register(User user) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(user.getUsername());
        List<User> list = userMapper.selectByExample(userExample);
        if (list.size() != 0) return -1;
        return userMapper.insertSelective(user);
    }

    @PostConstruct
    public void init() {
        System.out.println("UserServiceImpl：创建对象成功！");
    }


}
